package main

import "fmt"

// go语言中的没有单独的枚举类型，但是可以通过语言习惯实现

type ServerState int

// ServerState 的可能值定义为常量。特殊关键字 iota 自动生成连续的常量值;在这个 案例 0、1、2 等。
const (
	StateIdle ServerState = iota
	StateConnected
	StateError
	StateRetrying
)

var stateName = map[ServerState] string {
	StateIdle: "idle",
	StateConnected: "connected",
	StateError: "error",
	StateRetrying: "retrying",
}

func (ss ServerState) String() string{
	return stateName[ss]
}

func transition(s ServerState) ServerState{
	switch s {
	case StateIdle:
		return StateConnected
	case StateConnected, StateRetrying:
		return StateIdle
	case StateError:
		return StateError
	default:
		panic(fmt.Errorf("unknow state: %s",s))
	}
}


func main(){
	ns := transition(StateIdle)
	fmt.Println(ns)

	ns2 := transition(ns)
	fmt.Println(ns2)
}